package com.macllvm.whale.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.ImageButton;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.view.View.OnClickListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.util.LinkedList;
import java.util.concurrent.FutureTask;
import com.macllvm.whale.R;
import com.macllvm.whale.protocol.ProtocolController;
import com.macllvm.whale.entity.Transfer;

public class TransferAdapter extends BaseAdapter {
    private LinkedList<Transfer> transferList;
    private LayoutInflater inflater;
    private String[] stateDesc;

    static class TransferItemView{
        public boolean     isInit = false;
        public TextView    name;
        public TextView    state;
        public TextView    percent;
        public ProgressBar   progressbar;
        public ImageButton  btnStart;
        public ImageButton  btnCancel;
    }

    public TransferAdapter(Context ctx){
        inflater = LayoutInflater.from(ctx);
        stateDesc = new String[]{ ctx.getString(R.string.transfer_state_ready),
                                   ctx.getString(R.string.transfer_state_ongoing),
                                   ctx.getString(R.string.transfer_state_success),
                                   ctx.getString(R.string.transfer_state_fail),
                                   ctx.getString(R.string.transfer_state_cancel) };

    }

    public void setDataSource(LinkedList<Transfer> list){
        transferList = list;
    }

    @Override
    public int getCount() {
        return transferList.size();
    }

    @Override
    public Object getItem(int position) {
        return transferList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TransferItemView transferItemView;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.transfer_item, null);
            transferItemView = new TransferItemView();
            transferItemView.btnCancel = (ImageButton)convertView.findViewById(R.id.btn_cancel);
            transferItemView.btnStart = (ImageButton)convertView.findViewById(R.id.btn_start);
            transferItemView.name = (TextView)convertView.findViewById(R.id.file_name);
            transferItemView.state = (TextView)convertView.findViewById(R.id.transfer_state);
            transferItemView.progressbar = (ProgressBar)convertView.findViewById(R.id.progressbar);
            transferItemView.percent = (TextView)convertView.findViewById(R.id.transfer_percent);
            convertView.setTag(transferItemView);
        } else {
            transferItemView = (TransferItemView)convertView.getTag();
        }

        Transfer t = (Transfer)transferList.get(position);
        int begin = t.filePath.lastIndexOf("/") + 1;
        int end = t.filePath.length();
        transferItemView.name.setText( t.filePath.substring(begin, end) );

        transferItemView.percent.setText(Integer.toString(t.percent) + "%");
        transferItemView.state.setText(stateDesc[t.state]);
        transferItemView.progressbar.setProgress(t.percent);

        if (!transferItemView.isInit) {
            transferItemView.btnCancel.setOnClickListener(new BtnCancelOnClickListener(position));
            transferItemView.isInit = true;
        }
        transferItemView.btnCancel.setVisibility( View.VISIBLE );

        if(t.transferType == Transfer.TYPE_RECV ) {
            transferItemView.btnStart.setVisibility( View.GONE );
        }else{
            if(t.state != Transfer.STATE_ONGOING) {
                transferItemView.btnCancel.setVisibility( View.GONE );
            }
            transferItemView.btnStart.setVisibility( View.GONE );
        }

        if(t.state == Transfer.STATE_SUCCESS || t.state == Transfer.STATE_FAIL || t.state == Transfer.STATE_CANCEL) {
            transferItemView.btnCancel.setVisibility( View.GONE );
            transferItemView.btnCancel.setOnClickListener(null);
        }

        return convertView;
    }

    private class BtnCancelOnClickListener implements  OnClickListener {
        private int index;
        public BtnCancelOnClickListener(int i){
            index = i;
        }
        @Override
        public void onClick(View v){

            v.setEnabled(false);
            int type = transferList.get(index).transferType;


            if(type == Transfer.TYPE_RECV) {
                ProtocolController.getInstance().getTransferInManager().stopTask(index);
            }else{
                ProtocolController.getInstance().getTransferOutManager().stopTask(index);
            }
            v.setVisibility( View.GONE );
        }
    }

}
